package org.zyj.io.pipeline.duplex1;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPromise;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.nio.charset.StandardCharsets;

public class DuplexHandlerInitializer extends ChannelInitializer<NioSocketChannel> {

    @Override
    protected void initChannel(NioSocketChannel ch) throws Exception {
        ch.pipeline() //配置pipeline
                .addLast(new DuplexHandlerInitializer.DuplexHandler1())
                .addLast(new DuplexHandlerInitializer.DuplexHandler2())
                .addLast(new DuplexHandlerInitializer.DuplexHandler3())//这里writeAndFlush
                .addLast(new DuplexHandlerInitializer.DuplexHandler4())
        ;
    }

    //1
    static class DuplexHandler1 extends ChannelDuplexHandler {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ByteBuf byteBuf = (ByteBuf) msg;
            System.out.println("DuplexHandler1 channelRead; receive from = " + byteBuf.toString(StandardCharsets.UTF_8));
            ctx.fireChannelRead("handler1");
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("DuplexHandler1 write; receive from = " + msg.toString());
            ctx.writeAndFlush("handler1");
        }
    }

    //2
    static class DuplexHandler2 extends ChannelDuplexHandler {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("DuplexHandler2 channelRead; receive from = " + msg.toString());
            ctx.fireChannelRead("handler2");
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("DuplexHandler2 write; receive from = " + msg.toString());
            ctx.writeAndFlush("handler2");
        }
    }

    //3
    static class DuplexHandler3 extends ChannelDuplexHandler {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("DuplexHandler3 channelRead; receive from = " + msg.toString());
//            ctx.fireChannelRead("handler3");
            ctx.writeAndFlush("handler3");
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("DuplexHandler3 write; receive from = " + msg.toString());
            ctx.writeAndFlush("handler3");
        }
    }

    //4
    static class DuplexHandler4 extends ChannelDuplexHandler {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("DuplexHandler4 channelRead; receive from = " + msg.toString());
            ctx.fireChannelRead("handler4");
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("DuplexHandler4 write; receive from = " + msg.toString());
            ctx.writeAndFlush("handler4");
        }
    }
}